/**********************************************************************************
// DemoLine
//
// Creation:	16 Jul 2007
// Updated:		27 May 2011
// Compiler:	Visual Studio 2010
//
// Notes:		A simple project to test the DirectDrawGame class.
//				Test the line drawing functions of the engine.
//
**********************************************************************************/

#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "Resources.h"
#include "Anabele.h"	


/**********************************************************************************/
/*                           LineDemo Class                                       */
/**********************************************************************************/


class LineDemo: public D3DGame
{
private:
	Sprite * demobox;
	int x1, y1, x2, y2;
	int dirX1, dirY1, dirX2, dirY2;
	int r, g, b;
	int dirR, dirG, dirB;
	char text[80];

public:
	void GameInit();
	void GameLogic();
	void GameDraw();
	void GameShutDown();
	void UnloadResources();
	void LoadResources();
};

/**********************************************************************************/

void LineDemo::GameInit()
{
	srand(GetTickCount());

	x1 = rand() % GetWindowWidth();
	y1 = rand() % GetWindowHeight();
	x2 = rand() % GetWindowWidth();
	y2 = rand() % GetWindowHeight();

	dirX1 = 3;
	dirY1 = 3;
	dirX2 = 3;
	dirY2 = 3;

	r = rand()%256;
	g = rand()%256;
	b = rand()%256;

	dirR = 1;
	dirG = 1;
	dirB = 1;

	LoadResources();
}

/**********************************************************************************/

void LineDemo::LoadResources()
{
	demobox = new Sprite("Resources/demoBox.png");
	demobox->MoveTo(155, 100);
}

/**********************************************************************************/

void LineDemo::UnloadResources()
{
	if (demobox) delete demobox;
}

/**********************************************************************************/

void LineDemo::GameShutDown()
{
	UnloadResources();
}

/**********************************************************************************/

void LineDemo::GameLogic()
{
	// if user press ESC, exit game
	EndsOnEscape();

	x1 += dirX1;
	y1 += dirY1;
	x2 += dirX2;
	y2 += dirY2;

	if ((r == 255) & (dirR == 1))
		dirR = -1;

	if ((r == 0) & (dirR == -1))
		dirR = 1;

	if ((g == 255) & (dirG == 1))
		dirG = -1;

	if ((g == 0) & (dirG == -1))
		dirG = 1;

	if ((b == 255) & (dirB == 1))
		dirB = -1;

	if ((b == 0) & (dirB == -1))
		dirB = 1;

	r += dirR;
	g += dirG;
	b += dirB;

	if (x1 >= GetWindowWidth())
	{
		x1 = GetWindowWidth()-1;
		dirX1 = -dirX1;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (x2 >= GetWindowWidth())
	{
		x2 = GetWindowWidth()-1;
		dirX2 = -dirX2;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (y1 >= GetWindowHeight())
	{
		y1 = GetWindowHeight()-1;
		dirY1 = -dirY1;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (y2 >= GetWindowHeight())
	{
		y2 = GetWindowHeight()-1;
		dirY2 = -dirY2;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (x1 < 0)
	{
		x1 = 0;
		dirX1 = -dirX1;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (x2 < 0)
	{
		x2 = 0;
		dirX2 = -dirX2;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (y1 < 0)
	{
		y1 = 0;
		dirY1 = -dirY1;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}

	if (y2 < 0)
	{
		y2 = 0;
		dirY2 = -dirY2;
		r = rand()%256;
		g = rand()%256;
		b = rand()%256;
	}
} 

/**********************************************************************************/

void LineDemo::GameDraw()
{
	// lock screen (for pixel plotting functions)
	LockScreen();

	Line line = Line(float(x1),float(y1),float(x2),float(y2));
	line.color = Color (r,g,b);
	line.Draw();

	// unlock screen
	UnLockScreen();

	demobox->Draw();

	sprintf_s(text, "Line Demo");
	sysFont->Draw(55, 40, text);

	sprintf_s(text, "Testing line algorithm");
	sysFont->Draw(55, 80, text);

	sprintf_s(text, "Symmetric Double Step Line");
	sysFont->Draw(55, 100, text);

	sprintf_s(text, "Algorithm by Xialon Wu");
	sysFont->Draw(55, 120, text);

	sprintf_s(text, "Line from (%d,%d) to (%d,%d)", x1, y1, x2, y2);
	sysFont->Draw(55, 140, text);
} 



/**********************************************************************************/
/*                                  WinMain                                       */
/**********************************************************************************/

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	LineDemo lines;

	//lines.SetFramePerSec(VARIABLEFPS);
	//lines.SetWindowMode(WINDOWED);
	//lines.SetWindowSize(1024,768);
	lines.SetWindowBgColor(10,25,55);
	lines.SetWindowTitle(string("Line Demo"));
	lines.SetWindowIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)));
	lines.SetWindowCursor(LoadCursor(hInstance, MAKEINTRESOURCE(IDI_CURSOR)));
	lines.ShowWindowCursor(true);
	lines.Initialize(hInstance);
	lines.Start();
	
	return 0;

}

/**********************************************************************************/

